package data.dao.daoImplementation;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import data.dao.*;
import data.dto.*;
import data.searchdto.*;

public class LeaveTypeDAOImpl implements LeaveTypeDAO {

	public ArrayList<LeaveTypeDTO> findLeaveType(short id)
			throws LeaveDAOException, java.sql.SQLException, SQLException {
		PreparedStatement stmt = null;
		ArrayList<LeaveTypeDTO> searchResults = new ArrayList<LeaveTypeDTO>();
		ResultSet result = null;
		String sql = "SELECT * FROM leavetype WHERE isactive = 1 AND id =" + id;
		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			result = util.Database.getInstance().executeQuery(stmt);
			while (result.next()) {
				LeaveTypeDTO temp = new LeaveTypeDTO();
				temp.setId(result.getInt("id"));
				temp.setName(result.getString("name"));
				//temp.setCreatedBy(result.getInt("createdby"));
				//temp.setCreatedOn(result.getTimestamp("createdon"));
				//temp.setModifiedBy(result.getInt("modifiedby"));
				//temp.setModifiedOn(result.getTimestamp("modifiedon"));
				temp.setStatus(result.getShort("status"));
				searchResults.add(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (stmt != null)
				stmt.close();
		}

		return searchResults;
	}

	@Override
	public ArrayList<LeaveTypeDTO> findAllLeaveType() throws LeaveDAOException,
			java.sql.SQLException, SQLException {
		// TODO Auto-generated method stub
		ArrayList<LeaveTypeDTO> searchResults = null;
		try {
			String sql = "SELECT * FROM leavetype WHERE isactive = 1 ORDER BY id ASC ";

			searchResults = constructList(util.Database.getInstance()
					.getConnection().prepareStatement(sql));

		} finally {

		}
		return searchResults;
	}

	@Override
	public void insertLeaveDetailType(LeaveTypeDTO LeaveDetail)
			throws LeaveDAOException, java.sql.SQLException, SQLException {

		String sql = "";
		PreparedStatement stmt = null;

		try {
			sql = "INSERT INTO leavetype ( name, "
					+ "status,isactive) VALUES (?, ?,1) ";

			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);

			stmt.setString(1, LeaveDetail.getName());
			//stmt.setInt(2, util.CurrentUser.getInstance().getUserId());
			//stmt.setTimestamp(3, util.CurrentDate.getCurrentTimeStamp());
			//stmt.setInt(4, util.CurrentUser.getInstance().getUserId());
			//stmt.setTimestamp(5, util.CurrentDate.getCurrentTimeStamp());
			stmt.setInt(2, LeaveDetail.getStatus());

			int rowcount = util.Database.getInstance().executeUpdate(stmt);
			if (rowcount != 1) {
				// System.out.println("PrimaryKey Error when updating DB!");
				throw new SQLException("PrimaryKey Error when updating DB!");
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (stmt != null)
				stmt.close();
		}

	}

	@Override
	public void updateLeaveDetailType(LeaveTypeDTO LeaveDetail)
			throws LeaveDAOException, java.sql.SQLException, NotFoundException,
			SQLException {

		String sql = "UPDATE leavetype SET name = ?, "
				+ "status = ? WHERE (id = ? ) ";
		PreparedStatement stmt = null;

		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			stmt.setString(1, LeaveDetail.getName());
			//stmt.setInt(2, util.CurrentUser.getInstance().getUserId());
			//stmt.setTimestamp(3, util.CurrentDate.getCurrentTimeStamp());
			stmt.setInt(2, LeaveDetail.getStatus());
			stmt.setInt(3, LeaveDetail.getId());

			int rowcount = util.Database.getInstance().executeUpdate(stmt);
			if (rowcount == 0) {
				// System.out.println("Object could not be saved! (PrimaryKey not found)");
				throw new NotFoundException(
						"Object could not be saved! (PrimaryKey not found)");
			}
			if (rowcount > 1) {
				// System.out.println("PrimaryKey Error when updating DB! (Many objects were affected!)");
				throw new SQLException(
						"PrimaryKey Error when updating DB! (Many objects were affected!)");
			}
		} finally {
			if (stmt != null)
				stmt.close();
		}

	}

	@Override
	public void deleteLeaveDetailType(LeaveTypeDTO LeaveDetail)
			throws LeaveDAOException, java.sql.SQLException, NotFoundException,
			SQLException {

		String sql = "UPDATE leavetype SET isactive = 0 WHERE id = ?  ";
		PreparedStatement stmt = null;

		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			stmt.setInt(1, LeaveDetail.getId());

			int rowcount = util.Database.getInstance().executeUpdate(stmt);
			if (rowcount == 0) {
				// System.out.println("Object could not be deleted (PrimaryKey not found)");
				throw new NotFoundException(
						"Object could not be deleted! (PrimaryKey not found)");
			}
			if (rowcount > 1) {
				// System.out.println("PrimaryKey Error when updating DB! (Many objects were deleted!)");
				throw new SQLException(
						"PrimaryKey Error when updating DB! (Many objects were deleted!)");
			}
		} finally {
			if (stmt != null)
				stmt.close();
		}

	}

	protected ArrayList<LeaveTypeDTO> constructList(PreparedStatement stmt)
			throws SQLException, java.sql.SQLException {

		ArrayList<LeaveTypeDTO> searchResults = new ArrayList<LeaveTypeDTO>();
		ResultSet result = null;

		try {
			result = util.Database.getInstance().executeQuery(stmt);

			while (result.next()) {
				LeaveTypeDTO temp = new LeaveTypeDTO();

				temp.setId(result.getInt("id"));
				temp.setName(result.getString("name"));
				//temp.setCreatedBy(result.getInt("createdby"));
				//temp.setCreatedOn(result.getTimestamp("createdon"));
				//temp.setModifiedBy(result.getInt("modifiedby"));
				//temp.setModifiedOn(result.getTimestamp("modifiedon"));
				temp.setStatus((short) 1);

				searchResults.add(temp);
			}

		} finally {
			if (result != null)
				result.close();
			if (stmt != null)
				stmt.close();
		}

		return searchResults;
	}

	public ArrayList<LeaveTypeDTO> searchMatching(LeaveTypeDTO valueObject)
			throws SQLException, java.sql.SQLException {

		ArrayList<LeaveTypeDTO> searchResults;

		boolean first = true;
		StringBuffer sql = new StringBuffer(
				"SELECT * FROM leavetype WHERE isactive = 1 AND 1=1 ");

		if (valueObject.getId() != 0) {
			if (first) {
				first = false;
			}
			sql.append("AND id = ").append(valueObject.getId()).append(" ");
		}

		if (valueObject.getName() != null) {
			if (first) {
				first = false;
			}
			sql.append("AND name LIKE '").append(valueObject.getName())
					.append("%' ");
		}

		if (valueObject.getStatus() != 0) {
			if (first) {
				first = false;
			}
			sql.append("AND status = ").append(valueObject.getStatus())
					.append(" ");
		}

		sql.append("ORDER BY id ASC ");

		// Prevent accidential full table results.
		// Use loadAll if all rows must be returned.
		if (first)
			searchResults = new ArrayList<LeaveTypeDTO>();
		else
			searchResults = constructList(util.Database.getInstance()
					.getConnection().prepareStatement(sql.toString()));

		return searchResults;
	}

}
